package server

import (
	"mytest/Internal/api"
	"mytest/Internal/middleware"

	"github.com/gin-gonic/gin"
)

func SyncInitHTTP(port string) error {
	gin.SetMode(gin.DebugMode)
	r := gin.New()
	r.Use(gin.Recovery())
	r.Use(gin.Logger())
	r.Use(middleware.Cors())
	login := r.Group("")
	//login
	login.POST("/login", api.Login)
	login.POST("/register", api.Register)
	//业务,需要token 验证
	student := login.Group("/stu").Use(middleware.JWTAuth())
	student.POST("/add", api.CreateStu)
	student.POST("/del/:id", api.DeleteStu)
	student.POST("/update", api.UpdateStu)
	student.GET("/students", api.StudentList)
	student.POST("/downCsv", api.DownCsv)
	//jwt
	//用户
	student.POST("/setPassword", api.ChangePassword)
	student.POST("/logOut", api.LogOut)
	student.GET("/users", api.Users)
	student.PUT("/user/setAuthority", api.SetUserAuthority)
	student.POST("/killUser", api.DeleteUser)
	//casbin
	student.PUT("/casbin/update", api.UpdateCasbin)
	student.POST("/casbinById", api.GetCasbinById)
	//api 管理
	apix := login.Group("/api")
	apix.GET("/apis", api.AllApis)
	apix.POST("/add", api.CreateApi)
	apix.DELETE("/deleteApi")
	//角色权限
	student.POST("/createAuth", api.CreateAuth)   //创建角色
	student.POST("/setAuthMenu", api.SetAuthMenu) //给角色添加菜单
	student.GET("/getAuthority", api.GetAllAuthority)
	student.GET("/getAllAuthority", api.GetAllAuthorityFlat)
	student.DELETE("/deleteAuthority", api.DeleteAuthority)
	student.PUT("/updateAuth", api.UpdateAuthority)

	//菜单
	student.POST("/addMenu", api.AddBaseMenu)         //增加菜单
	student.DELETE("/deleteMenu", api.DeleteBaseMenu) //删除一个菜单
	student.PUT("/updateMenu", api.UpdateBaseMenu)    //更新一个菜单
	student.GET("/getMenuById", api.GetBaseMenu)      //查看一个菜单
	student.POST("/GetMenuList", api.GetMenuList)

	student.GET("/Menus", api.GetAllBaseMenu)               //获取所有菜单,树形结构
	student.POST("/menuForUser", api.GetMenusTreeForUser)   //获取用户的菜单树
	student.POST("/getMenuAuthority", api.GetMenuAuthority) //获取用户菜单列表

	//log.Println("run by localhost:8080")
	return r.Run(":" + port)
}
